package local.sumw3.findmydevices;

import java.util.ArrayList;
import java.util.List;

import local.sumw3.findmydeivces.R;
import local.sumw3.findmydevices.object.bean.Device;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.utils.CoordinateConver;
import com.baidu.platform.comapi.basestruct.GeoPoint;

public class DeviceItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
    private Context mContext;
    private GeoPoint mGeoPoint;

    public DeviceItemizedOverlay(Drawable marker, Context context, Device device) {
        super(marker);
        this.mContext = context;
        update(device);
    }

    public DeviceItemizedOverlay(Drawable marker, Context context) {
        super(marker);
        this.mContext = context;
    }

    public void update(Device device) {
        if (device != null && device.getLocation() != null) {
            GeoPoint oGeoPoint = new GeoPoint((int) (device.getLocation().getLatitude() * 1E6), (int) (device
                    .getLocation().getLongitude() * 1E6));
            mGeoPoint = CoordinateConver.fromWgs84ToBaidu(oGeoPoint);
            mGeoList.clear();
            mGeoList.add(new OverlayItem(mGeoPoint, device.getDeviceDisplayName(), device.getDeviceDisplayName()));
        } else {
            Toast.makeText(mContext, mContext.getResources().getString(R.string.msg_noLocationFound),
                    Toast.LENGTH_SHORT).show();
        }
        populate();
    }

    public GeoPoint getGeoPoint() {
        return mGeoPoint;
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mGeoList.get(i);
    }

    @Override
    public int size() {
        return mGeoList.size();
    }

    @Override
    // 处理当点击事件
    protected boolean onTap(int i) {
        Toast.makeText(this.mContext, mGeoList.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
        return true;
    }

}
